package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TGoodsOnedollarEntity;
import com.gmail.blackdog1987.onedollar.entity.TGoodsShowEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.GoodsOneDollarVO;
import com.gmail.blackdog1987.onedollar.vo.GoodsShowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository("goodsShowDao")
public class GoodsShowDao extends EntityHibernateDao<TGoodsShowEntity>{
    @Autowired
    private BaseJdbcDao baseJdbcDao;

    public Page<GoodsShowVO> queryForPage(int pageindex, int pagesize, int userid, int sort, int type){
        String whereSql;
        ArrayList<Object> whereArgs = new ArrayList<Object>();
        whereSql = " FROM t_goods_show s\n" +
                "LEFT JOIN t_goods g ON s.`goods_id`=g.`goods_id`\n" +
                "LEFT JOIN t_goods_picture p ON s.goods_id=p.goods_id and p.is_main = 1\n" +
                "LEFT JOIN t_user u ON u.user_id = s.user_id\n" +
                "LEFT JOIN t_goods_show_like l ON l.show_id = s.id AND l.user_id = ?\n" +
//                "LEFT JOIN t_goods_show_comment c ON c.show_id = s.id AND u.user_id = ?\n" +
                "LEFT JOIN t_goods_onedollar o ON s.onedollar_id = o.id\n" +
                "WHERE 1=1 ";
        whereArgs.add(Integer.valueOf(userid));
//        whereArgs.add(Integer.valueOf(userid));
        if(userid != 0 && type == 1){
            whereSql += " AND s.user_id = ? ";
            whereArgs.add(Integer.valueOf(userid));
        }

        if(sort==1){
            whereSql += " ORDER BY s.likes DESC ";
        }else if(sort==2){
            whereSql += " ORDER BY s.comments DESC ";
        }else{
            whereSql += " ORDER BY s.createtime DESC ";
        }

        String selectionSql = " SELECT s.*,\n" +
                "       p.`pic_url`,\n" +
                "       u.nick_name,\n" +
                "       l.id is_like,\n" +
                "       o.fill,\n" +
//                "       c.id is_comment,\n" +
                "       o.type,\n" +
                "       u.`head` ";

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;

        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + whereSql , whereArgs==null?null:whereArgs.toArray());

        List<GoodsShowVO> list = JdbcUtil.queryForList(baseJdbcDao, GoodsShowVO.class, selectionSql + whereSql + limit, whereArgs==null?null:whereArgs.toArray());

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }

    public Page<GoodsShowVO> queryForPage(int pageindex, int pagesize, int sort){
        return queryForPage(pageindex, pagesize, 0, sort, 0);
    }

    public GoodsShowVO query(int id, int userid){
        String sql = "SELECT \n" +
                "s.*,\n" +
                "p.`pic_url`,\n" +
                "u.nick_name,\n" +
                "l.id is_like,\n" +
                "o.fill,\n" +
                "o.type,\n" +
                "u.`head`,\n" +
                "r.onedollar_count\n" +
                "FROM t_goods_show s\n" +
                "LEFT JOIN t_goods g ON s.`goods_id`=g.`goods_id`\n" +
                "LEFT JOIN t_goods_picture p ON s.goods_id=p.goods_id and p.is_main = 1\n" +
                "LEFT JOIN t_user u ON u.user_id = s.user_id\n" +
                "LEFT JOIN t_goods_show_like l ON l.show_id = s.id AND l.user_id = ?\n" +
                "LEFT JOIN t_goods_onedollar o ON s.onedollar_id = o.id\n" +
                "LEFT JOIN (select count(*) onedollar_count, user_id, onedollar_id from t_order_onedollar group by onedollar_id, user_id) r ON r.onedollar_id = s.onedollar_id AND r.user_id = s.user_id\n" +
                "WHERE s.id = ?";
        List<GoodsShowVO> list = JdbcUtil.queryForList(baseJdbcDao, GoodsShowVO.class, sql, new Object[]{userid,id});

        return list==null||list.isEmpty()?null:list.get(0);
    }


}
